# MakeBy: LiuBoyuan
import os
import re
import sys

banner = """
 .----------------.  .----------------.  .----------------.  .----------------.  .----------------.  .-----------------. .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
| |  ________    | || |     ____     | || | ____    ____ | || |      __      | || |     _____    | || | ____  _____  | || |    ______    | || |  _________   | || |  _________   | |
| | |_   ___ `.  | || |   .'    `.   | || ||_   \  /   _|| || |     /  \     | || |    |_   _|   | || ||_   \|_   _| | || |  .' ___  |   | || | |_   ___  |  | || | |  _   _  |  | |
| |   | |   `. \ | || |  /  .--.  \  | || |  |   \/   |  | || |    / /\ \    | || |      | |     | || |  |   \ | |   | || | / .'   \_|   | || |   | |_  \_|  | || | |_/ | | \_|  | |
| |   | |    | | | || |  | |    | |  | || |  | |\  /| |  | || |   / ____ \   | || |      | |     | || |  | |\ \| |   | || | | |    ____  | || |   |  _|  _   | || |     | |      | |
| |  _| |___.' / | || |  \  `--'  /  | || | _| |_\/_| |_ | || | _/ /    \ \_ | || |     _| |_    | || | _| |_\   |_  | || | \ `.___]  _| | || |  _| |___/ |  | || |    _| |_     | |
| | |________.'  | || |   `.____.'   | || ||_____||_____|| || ||____|  |____|| || |    |_____|   | || ||_____|\____| | || |  `._____.'   | || | |_________|  | || |   |_____|    | |
| |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------'  '----------------' 
"""
windows = sys.platform.lower()
print(windows)


class Command:
    def __init__(self):
        self.system = windows
        self.commands = {
            "clear": {
                "win32": "cls",
                "liunx": "clear"
            }
        }

    @property
    def clear(self):
        return self.commands.get("clear").get(self.system)


class Console:
    def __init__(self, filename=None):
        self.command = Command()
        self.filename = filename
        self.iplist = []
        self.mapper = {}

    def banner(self):
        print(banner)

    def getFileName(self):
        os.system(self.command.clear)
        self.banner()

        filename = input("请输入你的域名文件名并回车[默认ret.txt]")
        filename = filename if filename != "" else "ret.txt"

        self.filename = filename

    def getAllIp(self):
        with open(self.filename, 'r', encoding='utf-8') as f:
            self.iplist = f.read().splitlines()
        print(self.iplist)

    def getIpDomain(self, row:str):
        print(row)
        try:
            a = re.findall("Ping (.*?)\[(.*?)\]", row)[0]

            self.mapper[a[0]] =  a[1]
        except IndexError:
            ...
    def clear(self):
        os.system(self.command.clear)

    def count(self):
        """
        次数统计
        :return:
        """
        count = {}
        self.clear()
        allsDomain , allsIp = list(self.mapper.keys()),list(self.mapper.values())
        print(allsDomain, allsIp)
        for i in allsDomain:
            theip = self.mapper.get(i)
            count[i] = allsIp.count(theip)
        print(count)
        return count
class Runner:
    def __init__(self):
        self.console = Console()
        self.task_link()

    def task_link(self):
        self.console.getFileName()
        self.console.getAllIp()
        lenght = len(self.console.iplist)
        for count, i in enumerate(self.console.iplist):
            self.console.clear()
            if i == "": continue
            print(f"{'* ' * count}{'x ' * (lenght - count)}[已完成{count}个还剩下{lenght - count}个]")
            # print(self.console.mapper)
            self.console.getIpDomain(i)


        text = ""
        for key, value in self.console.count().items():
            text += f"{key} {value}\n"

        with open("ret2.txt", 'w') as f:
            f.write(text)
        print("已经为你保存输出结果ret2.txt")

if __name__ == '__main__':
    Runner()
